package com.trex.modules.tbl.trainLog.web;

import com.jeesite.common.entity.Page;
import com.trex.modules.tbl.entity.TblXdLjzyjl;
import com.trex.modules.tbl.lineform.entity.TblXdZxxdctzd;
import com.trex.modules.tbl.lineform.service.TblXdZxxdctzdService;
import com.trex.modules.tbl.replyform.entity.TblXdJxchsd;
import com.trex.modules.tbl.service.TblXdLjzyjlService;
import com.trex.modules.utils.JsonGenerator;
import com.trex.modules.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.jeesite.common.web.BaseController;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Controller
@RequestMapping(value = "/tbl/tblTrainLog")
public class tblTrainLogController{

    @Autowired
    private TblXdZxxdctzdService tblXdZxxdctzdService;
    @Autowired
    private TblXdLjzyjlService tblXdLjzyjlService;


    /**
     * 获取数据
     */
    @ModelAttribute
    public TblXdLjzyjl get(String recId, boolean isNewRecord) {
        return tblXdLjzyjlService.get(recId, isNewRecord);
    }

    /**
     * 查询列表
     */
    //@RequiresPermissions("tbl:tblXdLjzyjl:view")
    @RequestMapping(value = {"list", ""})
    public String list(@RequestParam(value = "userid", required = false) String userid,
                       @RequestParam(value = "postid", required = false) String postid, TblXdLjzyjl tblXdLjzyjl, Model model, HttpServletRequest request) {
        int hours = new Date().getHours();
        if(hours >= 8 &&  hours < 19) {
            tblXdLjzyjl.setShift("2");
        }else {
            tblXdLjzyjl.setShift("1");
        }
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
//        logger.info("Received userid:\"!!!!!!!!!!!!!!!!!!!!!!!!!\"+\"______\"+ " + userid);

        // 如果 `userid` 从 URL 中传递，可以手动设置到 `tblXdLjzyjl`
        tblXdLjzyjl.setUserid(userid);
        tblXdLjzyjl.setStationTime(TimeUtils.getCurrentDate());
        String userId = JsonGenerator.extractAndDecodeAccessToken(request);

        List<TblXdZxxdctzd> lineList = tblXdZxxdctzdService.selectLine1(userId); ;
        model.addAttribute("lineList", lineList);

        List<TblXdZxxdctzd> lineList2 = new ArrayList<>();
        model.addAttribute("lineListByStation", lineList2);
        model.addAttribute("tblXdLjzyjl", tblXdLjzyjl);

        TblXdJxchsd tblXdJxchsd = new TblXdJxchsd();
        model.addAttribute("tblXdJxchsd",tblXdJxchsd);
        return "modules/tbl/trainLog/tblTrainLogList";
    }
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<TblXdLjzyjl> listData(TblXdLjzyjl tblXdLjzyjl, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        tblXdLjzyjl.setPage(new Page<>(request, response));
        String shift = tblXdLjzyjl.getShift(); //班制
        String userId = JsonGenerator.extractAndDecodeAccessToken(request);
        Date startTime = tblXdLjzyjl.getStartTime();
        Date endTime = tblXdLjzyjl.getEndTime();
        if (startTime!=null && endTime!=null) {
            //日期
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startFormat = formatter.format(startTime);
            String endFormat = formatter.format(endTime);

            tblXdLjzyjl.setUserid(userId);
            String strWhere = "a.STATION_TIME > = '"+startFormat+"' AND a.STATION_TIME < = '"+endFormat+"' AND A.TYPE_CODE='"+ tblXdLjzyjl.getTypeCode()+"' AND a.LINE_CODE IN ( SELECT DISTINCT XLBH FROM VZD_LJCDDZ B, TBL_ZD_LINE C WHERE B.CDBH = C.CDBH AND B.WORKER_ID = '"+tblXdLjzyjl.getUserid()+"' AND C.DELE_FLAG = '0' )";
            if(tblXdLjzyjl.getStationCode()!=null && !Objects.equals(tblXdLjzyjl.getStationCode(), "")){
                strWhere += "AND LEFT(A.LINE_CODE,2) = '"+tblXdLjzyjl.getStationCode()+"' ";
            }
            tblXdLjzyjl.getSqlMap().put("extWhere", strWhere);
            tblXdLjzyjl.setRemarks("1");
            Page<TblXdLjzyjl> page = tblXdLjzyjlService.findPage(tblXdLjzyjl);
            return page;
        }
        return null;

    }
    /**
     * 查询列表
     */
    //@RequiresPermissions("tbl:tblXdLjzyjl:view")
    @RequestMapping(value = {"tablelist", ""})
    public String tablelist(@RequestParam(value = "userid", required = false) String userid,
                       @RequestParam(value = "postid", required = false) String postid, TblXdLjzyjl tblXdLjzyjl, Model model, HttpServletRequest request) {

        return "modules/tbl/tableInfo/tableInfo";
    }
}
